import java.util.LinkedList;
import java.util.Queue;

public class BinaryTree {
    static class TreeNode {
        public char val;
        TreeNode left;
        TreeNode right;


        public TreeNode(char val) {
            this.val = val;

        }
    }

    public TreeNode CreateTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        D.left = H;
        return A;
    }

    void PreOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        PreOrder(root.left);
        PreOrder(root.right);
    }

    void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);

    }

    void lastOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        lastOrder(root.left);
        lastOrder(root.right);
        System.out.print(root.val + " ");
    }

    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }

    public int GetLeafNodeConnt(TreeNode root) {
        if (root.left == null && root.right == null) {
            return 1;
        }
        return GetLeafNodeConnt(root.left) + GetLeafNodeConnt(root.right);
    }

    public int getKLeveNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLeveNodeCount(root.left, k - 1) + getKLeveNodeCount(root.right, k - 1);
    }

    public int getHighNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHigh = getHighNodeCount(root.left);
        int rightHigh = getHighNodeCount(root.right);
        return leftHigh > rightHigh ? leftHigh + 1 : rightHigh + 1;

    }

    public TreeNode find(TreeNode root, char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode ret = find(root.left, val);
        if (ret != null) {
            return ret;
        }
        ret = find(root.right, val);
        if (ret != null) {
            return ret;
        }
        return null;
    }

    void levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return;
        }
        TreeNode cur = root;
        queue.offer(cur);
        while (cur != null) {
            queue.offer(cur.left);
            queue.offer(cur.right);
            System.out.print(cur.val + " ");
            queue.poll();
            cur = queue.peek();
        }
    }

    boolean isCompleteTree(TreeNode root) {
        if (root == null) {
            return false;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        TreeNode leftTree;
        TreeNode rightTree;
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if (node == null) {
                break;
            }
            leftTree = node.left;
            rightTree = node.right;
            queue.offer(leftTree);
            queue.offer(rightTree);
        }

        while (!queue.isEmpty()) {
            TreeNode cur = queue.peek();
            if (cur != null) {
                return false;
            } else {
                queue.poll();
            }
        }
        return true;
    }
}